#!/bin/bash

## Find the system definition we to guide us.
ASD='unknown'
for i in *.asd ; do
 if (( 0`svn propget release $i` > 0 )) ; then
   ASD=$i
 fi
done
if [ $ASD == 'unknown' ] ; then
  echo "ERROR: no *.asd file has release property set to 1"
  exit 1
fi

SYSTEM=`echo $i | sed 's/.asd//'`
VERSION=`perl -n -e 'print "$1\n" if /version "([.0-9]+)"/;' $ASD`
PARTS=`sed -n 's/;; Release: //p' $ASD`

RELEASES=/tmp/releases
STAGE=${SYSTEM}_$VERSION
TAPE=$RELEASES/$STAGE.tar.gz

echo Packing release into $TAPE via $STAGE
if [ -e $TAPE ] ; then
  echo $TAPE already exists, remove it if approprate.
  exit 1
fi

if [ -e $STAGE ] ; then
  echo $STAGE already exists, remove it please.
  exit 1
fi

if (( `svn status | wc -l` > 0 )) ; then
  echo ERROR: svn status suggests your not in synch with the repository.
  exit 1
fi

mkdir $STAGE
cp $PARTS $STAGE
tar czf $TAPE $STAGE
rm -rf $STAGE
(cd $RELEASES ; gpg -b -a `basename $TAPE`)
